<?php

namespace App\Admin\Controllers;

use App\Models\Greeting;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;

class GreetingController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header(trans('admin.greeting'));
            $content->description(trans('admin.list'));
            
            $content->body($this->grid()->render());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header(trans('admin.greeting'));
            $content->description(trans('admin.edit'));

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header(trans('admin.greeting'));
            $content->description(trans('admin.create'));

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Greeting::class, function (Grid $grid) {
            $grid->id(trans('admin.ID'))->sortable();
            //$grid->content(trans('admin.content'));
            $grid->content(trans('admin.content'))->display(function($content) {
                return str_limit($content, 30, '...');
            });
            $grid->status(trans('admin.status'))->editable('select', [0 => '启用', 1 => '禁用']);
            
            $grid->actions(function ($actions) {
                $actions->disableView();
            });

            //禁用导出按钮
            $grid->disableExport();
            //禁用筛选按钮
            $grid->disableFilter();

            $grid->tools(function ($tools) { 
                //关闭批量删除 
                $tools->batch(function ($batch) { 
                    $batch->disableDelete(); 
                });
            });
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Greeting::class, function (Form $form) {
            $form->display('id', 'ID');

            $form->text('content', trans('admin.content'))->rules('required')->addElementLabelClass('must-input');
            $form->radio('status', trans('admin.status'))->options(['0'=> trans('admin.enable'),'1'=> trans('admin.disable')])->rules('required')->addElementLabelClass('must-input');
            
            //去掉添加/编辑表单的重置按钮
            $form->disableReset();
        });
    }
}
